package com.qiyang.mq.controller;

import com.qiyang.mq.HelloSender;
import com.qiyang.mq.fanout.FanoutSender;
import com.qiyang.mq.topic.TopicSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created on 2018/6/8.
 *
 * @author sehuaqi.
 * 描述：
 */
@RestController
public class TestMQController {

    @Autowired
    private HelloSender sender;


    @Autowired
    private HelloSender sender2;


    @Autowired
    private TopicSender topicSender;

    @Autowired
    private FanoutSender fanoutSender;

    @RequestMapping("/testMQ")
    public String testMQ() {

        sender.send();

        return "success";
    }

    @RequestMapping("/testOneToManyMQ/{num}")
    public String testOneToManyMQ(@PathVariable("num") int num) {

        for (int i = 0; i < num; i++) {
            sender.send("发送的第" + (i + 1) + "条消息");
        }

        return "success";
    }

    @RequestMapping("/testManyToManyMQ/{num}")
    public String testManyToManyMQ(@PathVariable("num") int num) {

        for (int i = 0; i < num; i++) {
            sender.send("第一个发送者：发送的第" + (i + 1) + "条消息");

            sender2.send("第二个发送者：发送的第" + (i + 1) + "条消息");
        }

        return "success";
    }

    @RequestMapping("/testSendObj")
    public String testSendObj() {

        SysUser sysUser = new SysUser(1001, "admin", "管理员");

        sender.send(sysUser);

        return "success";
    }

    @RequestMapping("/testSendMQ/topic/{type}")
    public String testSendMQ(@PathVariable("type") String type) {

        if ("fanoutSender".equals(type)) {
            fanoutSender.send();
        } else if ("send1".equals(type)) {
            topicSender.send1();
        } else if ("send2".equals(type)) {
            topicSender.send2();
        } else {
            topicSender.send();
        }

        return "success";
    }

}
